@echo off
cd /d "%~dp0"
setlocal
set workspace=%~n0
set workspace=%workspace:xrar-=%
call:xrar-workspace %workspace%
pause
goto:eof

:xrar-workspace
if not exist "%~f1" goto:eof

setlocal
if not defined log set log=%~dpn1.log
if /i "%log%" == "%~dpn1.log" call:xdel "%log%"

rem RAR <command>  [ -<switches> ]  <archive>  [ <@listfiles...> ] [ <files...> ]  [ <path_to_extract\> ]

set RAR=%ProgramFiles%\WinRAR\Rar
set archive=%~nx1.rar
set afl=%~nx1.rar.afl
set xfl=%~nx1.rar.xfl

if exist %afl% (set files=@%afl%) else (set files=%~nx1\*.*)

set switches=-m5 -r -tl
if exist %xfl% set switches=%switches% -x@%xfl%

call:xdel %archive%

echo RAR a %switches% %archive% %files%
echo RAR a %switches% %archive% %files%>>%log%

call "%RAR%" a %switches% %archive% %files%
set xel=%ERRORLEVEL%
set xem=UNKNOWN EXIT VALUE
for /f "tokens=1* delims==" %%a in (rar-exit-values.txt) do if "%xel%" == "%%a" set xem=%%b

echo.
echo RAR=%xel% --^> %xem%
echo.

echo.>>%log%
echo RAR=%xel% --^> %xem%>>%log%
echo.>>%log%

if /i not "%log%" == "%~dpn1.log" goto:eof

set siono=N
set /p siono="abrir %log% (S/N) ? [%siono%] "
if /i "%siono%" == "S" (start /d %SystemRoot% notepad "%log%") else (del "%log%")
goto:eof

:xdel
if exist "%~f1" (echo del "%~f1" & del "%~f1" & echo.)
goto:eof
